<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Model\Goods;
use App\Http\Model\Menu;
use App\Http\Model\User;
use App\Http\Model\History;
use DB;
use Config;
use EasyWeChat\Foundation\Application;

class GoodsController extends Controller{
    //商品详情页
    public function detail($goods_id,Request $request){
        $goods = new Goods;
        $user = new User;
        $history = new History;
        $historyCount = $history->where("goods_id",$goods_id)->where("user_id",session("uid"))->count();
        if($historyCount){
            $nowTime = date("Y-m-d H:i:s");
            $history->where("goods_id",$goods_id)->where("user_id",session("uid"))->update(["updated_at" => $nowTime]);
        }else{
            $history->user_id = session("uid");
            $history->goods_id = $goods_id;
            $history->save();
        }
        $userInfo = $user->find(session('uid'));
        $goodsInfo = $goods->find($goods_id);
        $res = is_file("upload/user/"+session('uid')+".jpg");
        if(!$res){
            $this->ewm();
        }
        $wechat = app('wechat');
        $js = $wechat->js;
        $domain_name = Config::get('app.domain_name');
        return view("home.goods.detail", [
            "goodsInfo"=>$goodsInfo,
            "userInfo"=>$userInfo,
            "js"=>$js,
            "domain_name"=>$domain_name
        ]);
    }

    // 商品列表页
    public function lists(){
        $goods = new Goods;
        $goodsInfo = $goods->skip(0)->take(8)->get();
        return view("home.goods.lists", ["goodsInfo"=>$goodsInfo]);
    }

    // ajax获取商品
    public function getGoodsList(Request $request){
        $num = $request->input("num");
        $goods = new Goods;
        $goodsInfo = $goods->skip($num)->take(8)->get();
        foreach ($goodsInfo as $k => $v) {
            $goodsInfo[$k]["clothing_price"] = $v->clothingAttr->first()->price;
            $goodsInfo[$k]["clothing_pre_price"] = $v->clothingAttr->first()->pre_price;
            $goodsInfo[$k]["sum"] = count($v->comment);
        }
        echo json_encode($goodsInfo);die;
    }

    // 分类商品列表
    public function assort(){
        $menu = new Menu;
        $menuInfo = $menu->get();
        return view("home.goods.assort", ["menuInfo"=>$menuInfo]);
    }

    // 分类商品列表
    public function listsMenu($id){
        $goods = new Goods;
        $goodsInfo = $goods->where("pid", $id)->skip(0)->take(8)->get();
        return view("home.goods.lists-menu", ["goodsInfo"=>$goodsInfo,"pid"=>$id]);
    }

    // ajax获取分类商品
    public function getGoodsMenuList(Request $request){
        $num = $request->input("num");
        $pid = $request->input("pid");
        $goods = new Goods;
        $goodsInfo = $goods->where("pid", $pid)->skip($num)->take(8)->get();
        foreach ($goodsInfo as $k => $v) {
            $goodsInfo[$k]["clothing_price"] = $v->clothingAttr->first()->price;
            $goodsInfo[$k]["clothing_pre_price"] = $v->clothingAttr->first()->pre_price;
            $goodsInfo[$k]["sum"] = count($v->comment);
        }
        echo json_encode($goodsInfo);die;
    }

    // 获取子级分类
    public static function getMenu($id){
        $menu = new Menu;
        $menuInfo = $menu->where("pid",$id)->get();
        return $menuInfo;
    }
    //热销商品
    public function hotGoods(){
        $goods = new Goods;
        $goodsInfo = $goods->where('status','1')->where('goods_type','1')->orderBy('updated_at','desc')->get();
        return view('home.goods.hotGoods',["goodsInfo"=>$goodsInfo]);
    }


// -----------------------------------------------------
}
